package com.atguigu.controller;

import com.atguigu.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 1，@RequestBody :将请求体中的内容和控制器方法的形参进行绑定
 * 2、使用@RequestBody注解将json格式的请求参数转换为java对象
 * a>导入jackson的依赖
 * b>在SpringMVC的配置文件中设置k<mvc: annotation-driven />
 * c>在处理请求的控制器方法的形参位置，直接设置json格式的请求参数要转换的java类型的形参，
 * 使用@RequestBody注解标识即可
 */

@Controller
public class TestAjax {
    @RequestMapping("/test/ajax")
    public void testAjax(Integer id,@RequestBody String requestBody, HttpServletResponse response) throws IOException {
        System.out.println("id="+id);
        System.out.println("requestBody="+requestBody);
        response.getWriter().write("hello,axios");
    }

    //@RequestMapping("/test/RequestBody/json")
    public void testRequestBody(@RequestBody Map<String,Object>map, HttpServletResponse response) throws IOException {
        System.out.println(map);
        response.getWriter().write("hello,ResponseBody");
    }

    @RequestMapping("/test/RequestBody/json")
    public void testRequestBody(@RequestBody User user,HttpServletResponse response) throws IOException {
        System.out.println(user);
        response.getWriter().write("hello,ResponseBody");
    }
    @RequestMapping("/test/ResponseBody")
    @ResponseBody
    public List<User> testResponseBody(){
        User user1 = new User(1001, "admin1", "123456", 23, "男");
        User user2 = new User(1002, "admin2", "123456", 24, "男");
        User user3 = new User(1003, "admin3", "123456", 25, "男");
        User user4 = new User(1004, "admin4", "123456", 26, "男");
        List<User> list = Arrays.asList(user1, user2, user3, user4);
        return list;
    }
    /*@RequestMapping("/test/ResponseBody/json")
    @ResponseBody
    public Object testResponseBodyJson(){
        User user1 = new User(1001, "admin1", "123456", 23, "男");

        return user1;
    }*/

    @RequestMapping("/test/ResponseBody/json")
    @ResponseBody
    public Map<String,Object> testResponseBodyJson(){
        User user1 = new User(1001, "admin1", "123456", 23, "男");
        User user2 = new User(1002, "admin2", "123456", 24, "男");
        User user3 = new User(1003, "admin3", "123456", 25, "男");
        User user4 = new User(1004, "admin4", "123456", 26, "男");

        Map<String,Object> map=new HashMap<>();
        map.put("1001",user1);
        map.put("1002",user2);
        map.put("1003",user3);
        map.put("1004",user4);

        return map;
    }

    /*@RequestMapping("/test/ResponseBody/json")
    @ResponseBody
    public Object testResponseBodyJson(){
        User user1 = new User(1001, "admin1", "123456", 23, "男");
        User user2 = new User(1002, "admin2", "123456", 24, "男");
        User user3 = new User(1003, "admin3", "123456", 25, "男");
        User user4 = new User(1004, "admin4", "123456", 26, "男");

        Map<String,Object> map=new HashMap<>();

        List<User> users = Arrays.asList(user1, user2, user3, user4);

        map.put("code",200);
        map.put("data",users);
        map.put("msg","success");


        return map;
    }*/


}
